CUDADIR  ?= /usr/local/cuda
CUDASDK  ?= /usr/local/cudasdk
MPIDIR   ?= /usr/local

CC=g++ -m64
LD=g++ -m64
CFLAGS= -c -O3 -g  -DREAL=float -DSQRT=sqrtf -DEXP=expf -DCOS=cosf -DLOG=logf -DREALSCANFORMAT="%f" -DHOSTREDU -DREALENE=double -DEPSILON=0.000001f
#CFLAGS= -c -O3 -xS -fast -g  -DREAL=float -DSQRT=sqrtf -DEXP=expf -DCOS=cosf -DLOG=logf -DREALSCANFORMAT="%f" -DHOSTREDU -DREALENE=double -DEPSILON=0.000001f
#CFLAGS= -c -O3 -xS -fast -g -DREAL=double -DSQRT=sqrt -DEXP=exp -DCOS=cos -DLOG=log -DREALSCANFORMAT="%lf" -DHOSTREDU -DREALENE=double -DEPSILON=0.000001
#CFLAGS= -c -O3 -g -DREAL=double -DSQRT=sqrt -DEXP=exp -DCOS=cos -DLOG=log -DREALSCANFORMAT="%lf" -DHOSTREDU -DREALENE=double -DEPSILON=0.000001

MPILIB=$(MPIDIR)/lib
MPIINC=$(MPIDIR)/include

LDFLAGS= -lm -L$(MPILIB) -lmpi_cxx -lmpi
CUDACC=$(CUDADIR)/bin/nvcc

#CUDACFLAGS= -c -O3 -g -DREAL=double -DSQRT=sqrt -DEXP=exp -DCOS=cos -DLOG=log -DREALSCANFORMAT="%lf" --ptxas-options=-v -DHOSTREDU -DHEATBATH -DREALENE=double -DEPSILON=0.000001 -DGIORGIO -arch=sm_13 -DFERMI -Xptxas=-dlcm=ca -DNCOMP=3
CUDACFLAGS= -c -O3 -g -DREAL=float -DSQRT=sqrtf -DEXP=expf -DCOS=cosf -DLOG=logf -DREALSCANFORMAT="%f" --ptxas-options=-v -DHOSTREDU -DHEATBATH -DEPSILON=0.000001f -DREALENE=double -DGIORGIO -arch=sm_20 -DFERMI -Xptxas=-dlcm=ca -DMYREAL=float -DNCOMP=3 -ftz=true -prec-div=false -prec-sqrt=false
CUDAPTXFLAGS= -ptx -O3 -DREAL=float -DSQRT=sqrtf -DEXP=expf -DCOS=cosf -DLOG=logf -DREALSCANFORMAT="%f" --ptxas-options=-v -DHOSTREDU -DHEATBATH -DEPSILON=0.000001f -DREALENE=double -DGIORGIO -DFERMI -Xptxas=-dlcm=ca
CUDACUBINFLAGS= -cubin -O3 -G -g -DREAL=float -DSQRT=sqrtf -DEXP=expf -DCOS=cosf -DLOG=logf -DREALSCANFORMAT="%f" --ptxas-options=-v -DHOSTREDU -DHEATBATH -DEPSILON=0.000001f -DREALENE=double -DGIORGIO -arch=sm_20 -DFERMI -Xptxas=-dlcm=ca

CUDAINCLUDEDIR= $(CUDADIR)/include

CUDASDKIN=$(CUDASDK)/C/common/inc
CUDASDKLIB=$(CUDASDK)/C/lib
#CUDALDFLAGS=-L$(CUDADIR)/lib -L$(CUDASDKLIB) -lcudart -lcutil_i386 -arch=sm_13
CUDALDFLAGS=-L$(CUDADIR)/lib64 -L$(CUDASDKLIB) -lcudart -lcutil_x86_64 -arch=sm_20 -lcuda

all: isinggpuGM hsggpuGM hsggpuCA hsggpuCATB hsggpuSM hsggpuSM4P hsggpuTEXT hsggpuSM4PM hsgcpu Overrelax.cubin Overrelax.ptx hsgMGPUGM hsgAMGPUGM
all: isinggpuGM

hsgAMGPUGMos: hsgAMGPUGMos.o iniparser.o dictionary.o util.o hsgmpi.o
	${LD} -o $@ $^ ${LDFLAGS} ${CUDALDFLAGS}

isinggpuGM: isinggpuGM.cu_o iniparser.o dictionary.o util.o
#       ${LD} -o isinggpuGM isinggpuGM.o iniparser.o dictionary.o util.o ${LDFLAGS} ${CUDALDFLAGS}
	${LD} -o isinggpuGM isinggpuGM.o iniparser.o dictionary.o util.o ${LDFLAGS} ${CUDALDFLAGS}


hsggpuGM: hsggpuGM.cu_o threadFenceReduction_kernel.cu_o iniparser.o dictionary.o util.o 
#	${LD} -o hsggpuGM hsggpuGM.o threadFenceReduction_kernel.o iniparser.o dictionary.o util.o ${LDFLAGS} ${CUDALDFLAGS}
	${LD} -o hsggpuGM hsggpuGM.o iniparser.o dictionary.o util.o ${LDFLAGS} ${CUDALDFLAGS}

hsgMGPUGM: hsgMGPUGM.cu_o threadFenceReduction_kernel.cu_o iniparser.o dictionary.o util.o hsgmpi.o
#	${LD} -o hsgMGPUGM hsgMGPUGM.o threadFenceReduction_kernel.o iniparser.o dictionary.o util.o hsgmpi.o ${LDFLAGS} ${CUDALDFLAGS}
	${LD} -o hsgMGPUGM hsgMGPUGM.o iniparser.o dictionary.o util.o hsgmpi.o ${LDFLAGS} ${CUDALDFLAGS}

hsgAMGPUGM: hsgAMGPUGM.cu_o threadFenceReduction_kernel.cu_o iniparser.o dictionary.o util.o hsgmpi.o
#	${LD} -o hsgAMGPUGM hsgAMGPUGM.o threadFenceReduction_kernel.o iniparser.o dictionary.o util.o hsgmpi.o ${LDFLAGS} ${CUDALDFLAGS}
	${LD} -o hsgAMGPUGM hsgAMGPUGM.o iniparser.o dictionary.o util.o hsgmpi.o ${LDFLAGS} ${CUDALDFLAGS}

hsggpuTEXT: hsggpuTEXT.cu_o threadFenceReduction_kernel.cu_o iniparser.o dictionary.o util.o 
#	${LD} -o hsggpuTEXT hsggpuTEXT.o threadFenceReduction_kernel.o iniparser.o dictionary.o util.o ${LDFLAGS} ${CUDALDFLAGS}
	${LD} -o hsggpuTEXT hsggpuTEXT.o iniparser.o dictionary.o util.o ${LDFLAGS} ${CUDALDFLAGS}

hsggpuCA: hsggpuCA.cu_o threadFenceReduction_kernel.cu_o iniparser.o dictionary.o util.o 
#	${LD} -o hsggpuCA hsggpuCA.o threadFenceReduction_kernel.o iniparser.o dictionary.o util.o ${LDFLAGS} ${CUDALDFLAGS}
	${LD} -o hsggpuCA hsggpuCA.o iniparser.o dictionary.o util.o ${LDFLAGS} ${CUDALDFLAGS}

hsggpuCATB: hsggpuCATB.cu_o iniparser.o dictionary.o util.o 
#	${LD} -o hsggpuCATB hsggpuCATB.o threadFenceReduction_kernel.o iniparser.o dictionary.o util.o ${LDFLAGS} ${CUDALDFLAGS}
	${LD} -o hsggpuCATB hsggpuCATB.o iniparser.o dictionary.o util.o ${LDFLAGS} ${CUDALDFLAGS}

hsggpuSM: hsggpuSM.cu_o threadFenceReduction_kernel.cu_o iniparser.o dictionary.o util.o 
#       ${LD} -o hsggpuSM hsggpuSM.o threadFenceReduction_kernel.o iniparser.o dictionary.o util.o ${LDFLAGS} ${CUDALDFLAGS}
	${LD} -o hsggpuSM hsggpuSM.o iniparser.o dictionary.o util.o ${LDFLAGS} ${CUDALDFLAGS}

hsggpuSM4P: hsggpuSM4P.cu_o threadFenceReduction_kernel.cu_o iniparser.o dictionary.o util.o
#       ${LD} -o hsggpuSM4P hsggpuSM4P.o threadFenceReduction_kernel.o iniparser.o dictionary.o util.o ${LDFLAGS} ${CUDALDFLAGS}
	${LD} -o hsggpuSM4P hsggpuSM4P.o iniparser.o dictionary.o util.o ${LDFLAGS} ${CUDALDFLAGS}

hsggpuSM4PM: hsggpuSM4PM.cu_o threadFenceReduction_kernel.cu_o iniparser.o dictionary.o util.o
#       ${LD} -o hsggpuSM4P hsggpuSM4P.o threadFenceReduction_kernel.o iniparser.o dictionary.o util.o ${LDFLAGS} ${CUDALDFLAGS}
	${LD} -o hsggpuSM4PM hsggpuSM4PM.o iniparser.o dictionary.o util.o ${LDFLAGS} ${CUDALDFLAGS}

hsgcpu: hsgcpu.o loopxred.o loopxblue.o loopxredHB.o loopxblueHB.o iniparser.o dictionary.o util.o
	${LD} -o hsgcpu hsgcpu.o loopxred.o loopxblue.o loopxredHB.o loopxblueHB.o iniparser.o dictionary.o util.o ${LDFLAGS}

hsgmpi.o: hsgmpi.c
	mpic++ -c -m64 hsgmpi.c -DMYREAL=float -DREALENE=double -DNCOMP=3

.c.o:; $(CC) $(CFLAGS) $< -o $@

%.cu_o: %.cu
	$(CUDACC) $(CUDACFLAGS) -I$(CUDAINCLUDEDIR) -I$(CUDASDKIN) $(@:.cu_o=.cu) -o $(@:.cu_o=.o)

%.o: %.cu
	$(CUDACC) $(CUDACFLAGS) -I$(CUDAINCLUDEDIR) -I$(CUDASDKIN) $< -c -o $@

%.ptx: %.cu
	$(CUDACC) $(CUDAPTXFLAGS) -I$(CUDAINCLUDEDIR) -I$(CUDASDKIN) $(@:.ptx=.cu) -o $(@:.o=.ptx)

%.cubin: %.cu
	$(CUDACC) $(CUDACUBINFLAGS) -I$(CUDAINCLUDEDIR) -I$(CUDASDKIN) $(@:.cubin=.cu) -o $(@:.o=.cubin)

threadFenceReduction_kernel.o: threadFenceReduction_kernel.cu
	$(CUDACC) $(CUDACFLAGS) -I$(CUDAINCLUDEDIR) -I$(CUDASDKIN) threadFenceReduction_kernel.cu

clean:
	rm -rf *.o hsggpuGM hsggpuCA hsggpuSM hsggpuSM4P hsgcpu hsgMGPUGM hsgAMGPUGM hsgAMGPUGMos

.SUFFIXES: .cpp .cu .cu_dbg.o .c_dbg.o .cpp_dbg.o .cu_rel.o .c_rel.o .cpp_rel.o .cubin .ptx
.PRECIOUS: %.o
